package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.CondFrom;
import com.woniuxy.controller.form.ProjectIdNamePageForm;
import com.woniuxy.controller.form.ProjectNewFrom;
import com.woniuxy.controller.form.ProjectStateForm;
import com.woniuxy.controller.info.ProjectIdAndNameInfo;
import com.woniuxy.controller.info.ProjectInfo;
import com.woniuxy.controller.info.ProjectnameInfo;
import com.woniuxy.model.Project;
import com.woniuxy.model.RoleModel;
import com.woniuxy.service.ProjectService;
import com.woniuxy.service.UserService;
import com.woniuxy.utils.CopyUtil;
import com.woniuxy.utils.MyException;
import com.woniuxy.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@Api(tags = "项目表接口")
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Resource
    private ProjectService ps;
    @Resource
    private UserService userService;

    /**
     *  新建项目
     * @param projectNewFrom 项目信息对象
     * @param bindingResult 参数校验对象
     * @return
     * @throws MyException
     */
    @ApiOperation("新增项目")
    @PostMapping("/addnew")
    public Result addnewproject(@RequestBody @Valid ProjectNewFrom projectNewFrom,BindingResult bindingResult) throws MyException {
        System.out.println(projectNewFrom);
        if(bindingResult.hasErrors()){
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            String error = null;
            for (int i = 0; i < allErrors.size(); i++) {
                if(i<allErrors.size()-1){
                    error+=allErrors.get(i).getDefaultMessage()+",";
                }
            }
            throw new MyException(500,error);
        }
        boolean b = ps.addNewProject(projectNewFrom);
        if(!b) return Result.fail(500,"添加失败");
        return Result.success(200,"添加成功",null);
    }

    /**
     * 根据项目编号或者名称查询
     * @param condFrom  查询的项目信息对象
     * @param bindingResult  参数校验
     * @return  查询到的项目信息对象
     * @throws MyException
     */
    @ApiOperation("查询某个项目")
    @PostMapping("/getprojects")
    public Result getprojects(@RequestBody @Valid CondFrom condFrom,BindingResult bindingResult) throws MyException {
        System.out.println(condFrom);

        if(bindingResult.hasErrors()){
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            String error = null;
            for (int i = 0; i < allErrors.size(); i++) {
                if(i<allErrors.size()-1){
                    error+=allErrors.get(i).getDefaultMessage()+",";
                }
            }
            throw new MyException(500,error);
        }

        IPage<ProjectInfo> projectInfoList = ps.getprojects(condFrom);
        if(null == projectInfoList) return Result.fail(500,"查询失败");
        return Result.success(200,"查询成功",projectInfoList);
    }

    /**
     *  修改项目信息（名称、简介）
     * @param projectNewFrom 项目信息对象
     * @return
     */
    @ApiOperation("修改项目信息")
    @PostMapping("/changeproject")
    public Result changeproject(@RequestBody ProjectNewFrom projectNewFrom){
        System.out.println(projectNewFrom);

        boolean b = ps.changeproject(projectNewFrom);
        if(!b) return Result.fail(500,"修改失败");
        return Result.success(200,"修改成功",null);
    }

    /**
     * 关闭或启动项目
     * @param projectStateForm 项目
     * @return
     */
    @ApiOperation("关闭/打开项目")
    @PostMapping("changestate")
    public Result changestate(@RequestBody ProjectStateForm projectStateForm){
        System.out.println(projectStateForm);

        boolean b = ps.changestate(projectStateForm);
        if(!b) return Result.fail(500,"操作失败");
        return Result.success(200,"操作成功",null);
    }

    /**
     *  获取当前用户创建的所有项目
     * @param condFrom 当前登录用户的id
     * @return  返回所有项目信息
     * @throws MyException
     */
    @ApiOperation("查询项目名称")
    @PostMapping("/getprojectnames")
    public Result getprojectnames(@RequestBody CondFrom condFrom) throws MyException {
        RoleModel ismanager = userService.ismanager(condFrom.getUid());
        List<Project> list = null;
        if(ismanager.getRoleId()==1){
            list = ps.list();
        }else if(ismanager.getRoleId()==2){
            QueryWrapper<Project> wrapper = new QueryWrapper<>();
            wrapper.eq("project_createname_id",condFrom.getUid());
            list= ps.list(wrapper);
        }

        List<ProjectnameInfo> projectnameInfos = CopyUtil.copyList(list,ProjectnameInfo.class);
        System.out.println();
        if(null == projectnameInfos) throw new MyException(500,"未查到相关项目");
        return Result.success(200,"查询成功",projectnameInfos);
    }

    /**
     * 分页查询项目
     * @param projectIdNamePageForm 分页信息对象
     * @return  项目的查询结果
     */
    @ApiOperation("查询所有项目的名称和id并分页")
    @PostMapping("/getAllProjectByPage")
    public Result getAllProjectByPage(@RequestBody ProjectIdNamePageForm projectIdNamePageForm){
        Page<ProjectIdAndNameInfo> projectIdAndNameInfoPage=ps.getAllProjectByPage(projectIdNamePageForm);

        return Result.success(200,"查询成功",projectIdAndNameInfoPage);
    }

    /**
     *  查询项目的项目名信息
     * @return 项目信息
     */
    @ApiOperation("查询所有项目的名称")
    @PostMapping("/getAllProjectName")
    public Result getAllprojectName(){
        List<Project> allprojectName = ps.getAllprojectName();
        return Result.success(200,"success",allprojectName);
    }

    /**
     * 查询用户管理的项目的id和名称
     * @param uid  用户id
     * @return  项目信息
     */
    @ApiOperation(("查询用户管理的项目的id和名称"))
    @GetMapping("/getMyProjectName/{uid}")
    public Result getMyProjectName(@PathVariable Integer uid){

        List<ProjectIdAndNameInfo> projectIdAndNameInfos=ps.getMyProjectName(uid);
        return Result.success(200,"查询成功",projectIdAndNameInfos);
    }

    /**
     *  查询用户在的项目的id和名称
     * @param uid 用户id
     * @return 项目信息
     */
    @ApiOperation(("查询用户在的项目的id和名称"))
    @GetMapping("/getHaveMyProjectName/{uid}")
    public Result getHaveMyProjectName(@PathVariable Integer uid){
        List<ProjectIdAndNameInfo> projectIdAndNameInfos=ps.getHaveMyProjectName(uid);

        return Result.success(200,"查询成功",projectIdAndNameInfos);
    }

}

